<?php 
/*
Plugin Name: WPU Chunks
Plugin URI: http://tacoen.smedia.or.id/
Description: WPU themes bases template chunks<br/><small>This plugins is automatic activated when you selecting WPU themes at the first time.</small>
Author: Tacoen
Version: 0.5
Author URI: http://tacoen.smedia.or.id
*/

function ta_loadtext($file) {
		if ((! $file) || (filesize($file) < 1)) return;
		if(file_exists($file)) {
			$tf = fopen($file,r); while (!feof($tf)) { $tf_content .= fread($tf,filesize($file)); }
			fclose($tf); $ta_text = $tf_content;
			return $ta_text;
		}
}

function ta_savetext($file,$str_to_save) {
		$tafp = fopen("$file","w");
		fputs($tafp,$str_to_save,strlen($str_to_save));
		fclose($tafp);
}

function ta_chunk_browse() {
	if (file_exists(ta_incpath())) {

		echo "<div id='templateside'><h3>Files:</h3>
		<p>To use chunks in Sidebar Widget, you must name it with 'widget-'</p>
		<ul style='margin-left:0'>";

		if ($handle = opendir(ta_incpath())) {
			while (false !== ($file = readdir($handle))) {
				if ( substr($file, strrpos ($file, '.')) == ".php" || substr($file, strrpos ($file, '.')) == ".css") {
					$filelist[$countfile] = $file; $countfile++; 
				}
			}
		}
		closedir($handle);

   	sort($filelist,SORT_STRING); foreach($filelist as $file) {		
			echo "<li style='margin:.3em 0'><small>[<a style='color: #f00; text-decoration: none;' title='delete' href='?page=wpu-chunk_plugins.php&chunk=$file&ta_submit=DEL3TE'>X</a>]</small> <a href='?page=wpu-chunk_plugins.php&chunk=$file'>$file</a></li>"; 
		}

		echo "</div>\n";
	} else {

	echo ta_incpath() ." is not found! Please create it before continue.";
	die;
	}
}

function safe_load($content) {
	$tags = array(
		'<textarea>'=> '&lt;textarea&gt;',
		'</textarea>'=> '&lt;/textarea&gt;',
		'\"'=>'"',
		"\'"=>"'",
	);

	return str_replace(array_keys($tags), array_values($tags), $content);
}

function safe_save($content) {
	$tags = array(
		'&lt;textarea&gt;'=> '<textarea>',
		'&lt;/textarea&gt;'=> '</textarea>',
		'\"'=>'"',
		"\'"=>"'",
	);

	return str_replace(array_keys($tags), array_values($tags), $content);
}

function ta_chunk_editor($file) {

	$textcont ="";
	if (($file) && ($file != "")) { $textcont = safe_load(ta_loadtext(ta_incpath().$file)); }

	echo "<form id='template' method='post' action=''>";
	echo "<h3>File: <span style='border-bottom: 1px dotted #999'>$file</span></h3>";
	echo "<div>\n<textarea cols='72' rows='35' name='newcontent' id='newcontent' tabindex='2'>$textcont</textarea>\n</div>";
	echo "<p>Filename <span style='cursor: help;' title='Only .php and .css'>[?]</span> :"; 
	echo "<input type='text' value='$file' size='20' name='chunk' tabindex='1'></p>\n";
 	echo "<p class='submit' style='text-align: left'><input type='submit' name='ta_submit' value='Save' tabindex='3' /></p>\n";
 	echo "</form>\n<div class='clear'>&nbsp;</div>\n";

}

function chunk_edit_page() {

	if ( function_exists('ta_include')) {

	if ( $_GET['page'] == basename(__FILE__) ) {

	$file = $_REQUEST['chunk'];

	if ($file) {
		if (substr($file, strrpos ($file, '.')) !== ".css") { 
			if (substr($file, strrpos ($file, '.')) !== ".php") { $file .=".php"; }
	  }
		if ($_REQUEST['ta_submit']=="Save") {
			 ta_savetext(ta_incpath().$file,safe_save($_REQUEST['newcontent'])); 
		} else if ($_REQUEST['ta_submit']=="DEL3TE") {
			 if (file_exists(ta_incpath().$file)) { unlink(ta_incpath().$file); }
			 $file="";
		}			
	}

	echo "<div class='wrap'><h2>WPU Chunk:</h2>\n";

	ta_chunk_browse();
	ta_chunk_editor($file);

	}} else {

	echo "<div class='wrap'><p>Sorry, your selected themes doesn't use Template chunk.</p></div>";		
	
	}
}

function ta_register_wpuchunk() {
	add_theme_page('Template Chunks', 'WPU Chunks', 'edit_themes', basename(__FILE__), 'chunk_edit_page');
}

add_action('admin_menu', 'ta_register_wpuchunk');

?>